package com.Mms.action;


import com.Mms.model.*;
import com.Mms.service.serviceBySave;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Save")
public class saveByAllAction {

    @Autowired
    private  serviceBySave serviceBySave;

    /***
     * 新增用户信息，协会副会长 和 社联办公室副主任才有此权利
     * @param uname
     * @param usno
     * @param password
     * @param quanxian
     * @param mno
     * @param sex
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/saveMuser",method = RequestMethod.POST)
    public  Map saveMuser(@RequestParam(value = "uname") String uname, @RequestParam(value = "usno") String usno,
                   @RequestParam(value = "upassword") String password, @RequestParam(value = "quanxian") int quanxian,
                   @RequestParam(value = "mno") String mno, @RequestParam(value = "sex") String sex, HttpServletResponse response, HttpServletRequest request)throws IOException {
        request.setCharacterEncoding("UTF-8");
        Muser muser = new Muser();
        Date date = new Date();
        muser.setMdate(date);
        muser.setUname(uname);
        muser.setUstuno(usno);
        muser.setUpassword(password);
        muser.setQuanxian(quanxian);
        muser.setSex(sex);
        muser.setMno(mno);
        muser.setUbool(0);
        muser.setUlogic(0);
        Map<String,Object> stringObjectMap = new HashMap<>();
     if(serviceBySave.saveByMuser(muser))
         stringObjectMap.put("resultCode",200);
      else {
         stringObjectMap.put("resultCode",201);
     }
      return  stringObjectMap;
    }

    /**
     * 新增 社团信息，申请人被指定为协会会长 ustno为申请人的学号
     * @param mname
     * @param mno
     * @param minfo
     */
    @RequestMapping(value = "/saveMinfo",method = RequestMethod.POST)
    public void saveMajor(@RequestParam(value = "mname") String mname,@RequestParam(value = "mno") String mno,
                          @RequestParam(value = "minfo") String minfo, @RequestParam(value = "ustno") String ustno,
                          HttpServletResponse response){

    Minfo minfo1 = new Minfo();
     minfo1.setMname(mname);
     minfo1.setMno(mno);
     minfo1.setMinfo(minfo);
    serviceBySave.saveByMinfo(minfo1,ustno);
    }

    /**
     * 审核成员接口，flag值 说明  1 协会会长审核 2社联办公室副主任审核
     * @param ustuno
     * @param flag 返回值 1 成功， 2失败
     * @param response
     */
    @RequestMapping(value = "/updateMuser",method = RequestMethod.POST)
    public void updateMuser(@RequestParam(value = "ustuno") String ustuno,@RequestParam(value = "flag") int flag,
                          HttpServletResponse response,HttpServletRequest request)throws IOException {

        request.setCharacterEncoding("UTF-8");
            if(serviceBySave.updataMuser(ustuno,flag)) {
                outputformat.write(response, "1");
            }else
            {
                outputformat.write(response,"2");
            }

    }

    /**
     *  传入各中参数， 参数的含义 见sql文件的注解  消息发布接口
     *   返回值说明 1代表发布成果 2 发布失败
     * @param theme
     * @param type
     * @param toorgan
     * @param time
     * @param site
     * @param content
     * @param response
     */

    @ResponseBody
    @RequestMapping(value ="/sendMeassage",method = RequestMethod.POST)
     public  Map sendMeassage(@RequestParam(value = "Theme") String theme,@RequestParam(value = "Type") int type,
                               @RequestParam(value = "Toorgan") String toorgan,@RequestParam(value = "Time") String time,
                               @RequestParam(value = "Site") String site,@RequestParam(value = "Content") String content,
                               HttpServletResponse response,HttpServletRequest request)throws IOException {

        request.setCharacterEncoding("UTF-8");
        Anymessage anymessage =new Anymessage();
        anymessage.setContent(content);
        anymessage.setSite(site);
        anymessage.setTheme(theme);
        anymessage.setType(type);
        anymessage.setTime(time);
        anymessage.setToorgan(toorgan);
       System.out.println("发布成功");
        Map<String,Object> stringObjectMap = new HashMap<>();
        if(serviceBySave.saveAnymessage(anymessage)) {
            stringObjectMap.put("resultCode", 200);
            System.out.println("发布成功");

        }else {
            stringObjectMap.put("resultCode",201);
        }
        return  stringObjectMap;

    }


}
